//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.alarm.controller;

import ciicos.alarm.entity.AlarmRealNDataEntity;
import ciicos.alarm.model.alarmrealndata.AlarmRealNDataInfoVO;
import ciicos.alarm.model.alarmrealndata.AlarmRealNDataListVO;
import ciicos.alarm.model.alarmrealndata.AlarmRealNDataPagination;
import ciicos.alarm.service.AlarmRealNDataService;
import ciicos.base.ActionResult;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"新实时报警"},
    value = "alarmManage"
)
@RequestMapping({"/api/alarmManage/AlarmRealNData"})
public class AlarmRealNDataController {
    private static final Logger log = LoggerFactory.getLogger(AlarmRealNDataController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private AlarmRealNDataService alarmRealNDataService;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody AlarmRealNDataPagination alarmRealNDataPagination) throws IOException {
        List<AlarmRealNDataEntity> list = this.alarmRealNDataService.getList(alarmRealNDataPagination);
        List<AlarmRealNDataListVO> listVO = JsonUtil.getJsonToList(list, AlarmRealNDataListVO.class);

        for(AlarmRealNDataListVO alarmRealNDataVO : listVO) {
            alarmRealNDataVO.setTagname_name(alarmRealNDataVO.getTagname());
            alarmRealNDataVO.setTagdesc_name(alarmRealNDataVO.getTagdesc());
            alarmRealNDataVO.setTagtype_name(alarmRealNDataVO.getTagtype());
            alarmRealNDataVO.setTageu_name(alarmRealNDataVO.getTageu());
            alarmRealNDataVO.setTagarea_name(alarmRealNDataVO.getTagarea());
            alarmRealNDataVO.setTagunit_name(alarmRealNDataVO.getTagunit());
            alarmRealNDataVO.setAlarmtype_name(this.generaterSwapUtil.getDicName(alarmRealNDataVO.getAlarmtype(), "451412507504957189"));
            alarmRealNDataVO.setAlarmtime_name(alarmRealNDataVO.getAlarmtime() != null ? new Date(alarmRealNDataVO.getAlarmtime()) : null);
            alarmRealNDataVO.setAlarmv_name(alarmRealNDataVO.getAlarmv());
            alarmRealNDataVO.setAlarmlimitv_name(alarmRealNDataVO.getAlarmlimitv());
            alarmRealNDataVO.setAlarmsp_name(alarmRealNDataVO.getAlarmsp());
            alarmRealNDataVO.setAlarmlevel_name(this.generaterSwapUtil.getDicName(alarmRealNDataVO.getAlarmlevel(), "451405363372119813"));
            alarmRealNDataVO.setCreatoruserid_name(alarmRealNDataVO.getCreatoruserid());
            alarmRealNDataVO.setCreatortime_name(alarmRealNDataVO.getCreatortime() != null ? new Date(alarmRealNDataVO.getCreatortime()) : null);
            alarmRealNDataVO.setReturntime_name(alarmRealNDataVO.getReturntime() != null ? new Date(alarmRealNDataVO.getReturntime()) : null);
            alarmRealNDataVO.setReturnv_name(alarmRealNDataVO.getReturnv());
            alarmRealNDataVO.setLastmodifyuserid_name(this.generaterSwapUtil.userSelectValue(alarmRealNDataVO.getLastmodifyuserid()));
            alarmRealNDataVO.setLastmodifytime_name(alarmRealNDataVO.getLastmodifytime());
            alarmRealNDataVO.setConfirm_name(this.generaterSwapUtil.getDicName(alarmRealNDataVO.getConfirm(), "413671997705289733"));
            alarmRealNDataVO.setSendmark_name(this.generaterSwapUtil.getDicName(alarmRealNDataVO.getSendmark(), "415081677472137605"));
            alarmRealNDataVO.setEnabledmark_name(alarmRealNDataVO.getEnabledmark());
            alarmRealNDataVO.setAlarmstatus_name(this.generaterSwapUtil.getDicName(alarmRealNDataVO.getAlarmstatus(), "459255239141230853"));
            alarmRealNDataVO.setConfirmdesc_name(alarmRealNDataVO.getConfirmdesc());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(alarmRealNDataPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @DeleteMapping({"/batchRemove/{ids}/{desc}"})
    @Transactional
    @ApiOperation("批量确认")
    public ActionResult batchRemove(@PathVariable("ids") String ids, @PathVariable("desc") String desc) {
        String[] idList = ids.split(",");
        int i = 0;

        for(String allId : idList) {
            this.delete(allId, desc);
            ++i;
        }

        return i == 0 ? ActionResult.fail("确认失败") : ActionResult.success("确认成功");
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<AlarmRealNDataInfoVO> info(@PathVariable("id") String id) {
        AlarmRealNDataEntity entity = this.alarmRealNDataService.getInfo(id);
        AlarmRealNDataInfoVO vo = (AlarmRealNDataInfoVO)JsonUtil.getJsonToBean(entity, AlarmRealNDataInfoVO.class);
        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        if (vo.getLastmodifytime() != null) {
            vo.setLastmodifytime(vo.getLastmodifytime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<AlarmRealNDataInfoVO> detailInfo(@PathVariable("id") String id) {
        AlarmRealNDataEntity entity = this.alarmRealNDataService.getInfo(id);
        AlarmRealNDataInfoVO vo = (AlarmRealNDataInfoVO)JsonUtil.getJsonToBean(entity, AlarmRealNDataInfoVO.class);
        vo.setTagname(vo.getTagname());
        vo.setTagdesc(vo.getTagdesc());
        vo.setTagtype(vo.getTagtype());
        vo.setTageu(vo.getTageu());
        vo.setTagarea(vo.getTagarea());
        vo.setTagunit(vo.getTagunit());
        vo.setAlarmtype(this.generaterSwapUtil.getDicName(vo.getAlarmtype(), "451412507504957189"));
        vo.setAlarmtime(vo.getAlarmtime());
        vo.setAlarmv(vo.getAlarmv());
        vo.setAlarmlimitv(vo.getAlarmlimitv());
        vo.setAlarmsp(vo.getAlarmsp());
        vo.setAlarmlevel(this.generaterSwapUtil.getDicName(vo.getAlarmlevel(), "451405363372119813"));
        vo.setCreatoruserid(vo.getCreatoruserid());
        vo.setCreatortime(vo.getCreatortime());
        vo.setReturntime(vo.getReturntime());
        vo.setReturnv(vo.getReturnv());
        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        vo.setLastmodifytime(vo.getLastmodifytime());
        vo.setConfirm(this.generaterSwapUtil.getDicName(vo.getConfirm(), "413671997705289733"));
        vo.setSendmark(this.generaterSwapUtil.getDicName(vo.getSendmark(), "415081677472137605"));
        vo.setEnabledmark(vo.getEnabledmark());
        vo.setAlarmstatus(this.generaterSwapUtil.getDicName(vo.getAlarmstatus(), "456723697751820677"));
        return ActionResult.success(vo);
    }

    @ApiOperation("确认")
    @DeleteMapping({"/{id}/{desc}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id, @PathVariable("desc") String desc) {
        AlarmRealNDataEntity entity = this.alarmRealNDataService.getInfo(id);
        if (entity != null && StringUtil.isNotEmpty(desc)) {
            entity.setConfirmdesc(desc);
            this.alarmRealNDataService.confirm(entity);
            return ActionResult.success("确认成功");
        } else {
            return ActionResult.fail("参数不全或者修改失败，请检查");
        }
    }

    @ApiOperation("报警通知")
    @GetMapping({"/notice"})
    @DSTransactional
    public ActionResult notice(@RequestParam(name = "id",required = true) final String id, @RequestParam(name = "tenant",required = false) final String tenant) {
        log.info("接收到报警通知,id:{},tenant:{}", id, tenant);
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @SneakyThrows
            public void run() {
                    AlarmRealNDataController.log.info("接收到报警通知,推送通知");
                    AlarmRealNDataController.this.alarmRealNDataService.doSendMessage(id, tenant);

            }
        };
        timer.schedule(task, 5000L);
        return ActionResult.success();
    }
}
